// +----------------------------------------------------------------------
// | JavaWeb_EleVue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2021 上海JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <javaweb520@gmail.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package com.javaweb.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.*;
import com.javaweb.admin.mapper.KdlSineGoodsmovementBMapper;
import com.javaweb.admin.service.INcServiceLogService;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.SpecifyDataSource;
import com.javaweb.common.utils.CommonUtils;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.admin.constant.KdlSineGoodsmovementConstant;
import com.javaweb.admin.mapper.KdlSineGoodsmovementMapper;
import com.javaweb.admin.query.KdlSineGoodsmovementQuery;
import com.javaweb.admin.service.IKdlSineGoodsmovementService;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.utils.ShiroUtils;
import com.javaweb.admin.vo.kdlsinegoodsmovement.KdlSineGoodsmovementInfoVo;
import com.javaweb.admin.vo.kdlsinegoodsmovement.KdlSineGoodsmovementListVo;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
  * <p>
  * 信谊下发移库指令 服务类实现
  * </p>
  *
  * @author 鲲鹏
  * @since 2024-02-01
  */
@Service
public class KdlSineGoodsmovementServiceImpl extends BaseServiceImpl<KdlSineGoodsmovementMapper, KdlSineGoodsmovement> implements IKdlSineGoodsmovementService {

    @Autowired
    private KdlSineGoodsmovementMapper kdlSineGoodsmovementMapper;
    @Autowired
    private KdlSineGoodsmovementBMapper kdlSineGoodsmovementBMapper;
    @Autowired
    INcServiceLogService ncServiceLogService;
    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getList(BaseQuery query) {
        KdlSineGoodsmovementQuery kdlSineGoodsmovementQuery = (KdlSineGoodsmovementQuery) query;
        // 查询条件
        QueryWrapper<KdlSineGoodsmovement> queryWrapper = new QueryWrapper<>();
        // queryWrapper.orderByDesc("id");
        if(StringUtils.isNull(kdlSineGoodsmovementQuery.getBilltype())){
            kdlSineGoodsmovementQuery.setBilltype("");
        }

        queryWrapper.eq("billtype",kdlSineGoodsmovementQuery.getBilltype());

        // 获取数据列表
        IPage<KdlSineGoodsmovement> page = new Page<>(kdlSineGoodsmovementQuery.getPage(), kdlSineGoodsmovementQuery.getLimit());
        IPage<KdlSineGoodsmovement> pageData = kdlSineGoodsmovementMapper.selectPage(page, queryWrapper);
//        pageData.convert(x -> {
//            KdlSineGoodsmovementListVo kdlSineGoodsmovementListVo = Convert.convert(KdlSineGoodsmovementListVo.class, x);
//            return kdlSineGoodsmovementListVo;
//        });
        return JsonResult.success(pageData);
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getMovementList(BaseQuery query) {
        KdlSineGoodsmovementQuery kdlSineGoodsmovementQuery = (KdlSineGoodsmovementQuery) query;
        // queryWrapper.orderByDesc("id");
        if(StringUtils.isNull(kdlSineGoodsmovementQuery.getBilltype())){
            kdlSineGoodsmovementQuery.setBilltype("");
        }
        // 获取数据列表
        IPage<KdlSineGoodsmovement> page = new Page<>(kdlSineGoodsmovementQuery.getPage(), kdlSineGoodsmovementQuery.getLimit());
        IPage<KdlSineGoodsmovement> pageData = kdlSineGoodsmovementMapper.getGoodsmovementList(page, kdlSineGoodsmovementQuery);
        return JsonResult.success(pageData);
    }

    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public Object getInfo(Serializable id) {
        KdlSineGoodsmovement entity = (KdlSineGoodsmovement) super.getInfo(id);
        // 返回视图Vo
        KdlSineGoodsmovementInfoVo kdlSineGoodsmovementInfoVo = new KdlSineGoodsmovementInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, kdlSineGoodsmovementInfoVo);
        return kdlSineGoodsmovementInfoVo;
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult create(KdlSineGoodsmovementDto dto) {
        try
        {
            KdlSineGoodsmovement entity = dto.getGoodsmovement();
            String orderDate = DateUtil.format(DateUtil.parseDate(entity.getOrderdate()),DateUtils.YYYY_MM_DD);
            entity.setOrderdate(orderDate);
            entity.setPrincipalid(CommonConfig.kdlCompanyId);
            entity.setOperName(ShiroUtils.getUserName());
            entity.setStatus("未下发");
            entity.setOrderno(kdlSineGoodsmovementMapper.getOrderNo());
            kdlSineGoodsmovementMapper.insert(entity);
            for (KdlSineGoodsmovementB detail:dto.getGoodsmovementLines()) {
                detail.setPid(entity.getId());
                kdlSineGoodsmovementBMapper.insert(detail);
            }
            return JsonResult.success();
        }
        catch(Exception ex)
        {
            return JsonResult.error(ex.getMessage());
        }
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult update(KdlSineGoodsmovementDto dto) {
        try {
            KdlSineGoodsmovement entity = dto.getGoodsmovement();
            kdlSineGoodsmovementMapper.updateById(entity);
            //删除之前的明细
            Map<String,Object> param = new HashMap<>();
            param.put("pid",entity.getId());
            kdlSineGoodsmovementBMapper.deleteByMap(param);
            for (KdlSineGoodsmovementB detail : dto.getGoodsmovementLines()) {
                detail.setPid(entity.getId());
                kdlSineGoodsmovementBMapper.insert(detail);
            }
            return JsonResult.success();
        }
        catch (Exception ex)
        {
            return JsonResult.error(ex.getMessage());
        }
    }

    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult getMovementDetail(Integer pid) {
        List<KdlSineGoodsmovementB> list= kdlSineGoodsmovementMapper.getMovementDetail(pid);
        return JsonResult.success(list);
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult edit(KdlSineGoodsmovement entity) {
        if (StringUtils.isNotNull(entity.getId()) && entity.getId() > 0) {
        } else {
        }
        return super.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    @SpecifyDataSource(value = DataSourceType.SINESYS)
    public JsonResult delete(KdlSineGoodsmovement entity) {
        entity.setMark(0);
        return super.delete(entity);
    }

}